package com.bykj.property.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bykj.property.controller.vo.request.SysUserPageRequest;
import com.bykj.property.controller.vo.view.SysUserVo;
import com.bykj.property.sys.dao.SysUserDao;
import com.bykj.property.model.SysUser;
import com.bykj.property.mapper.SysUserMapper;
import com.bykj.property.service.SysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bykj.property.util.Md5HashUtil;
import com.bykj.property.util.RandomUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* <p>
    * 系统用户 服务实现类
    * </p>
*
* @author liangyiming
* @since 2019-06-20
*/
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {
@Autowired
private SysUserMapper mapper;
    @Autowired
private SysUserDao dao;
@Override
public Page<SysUser> queryPage(SysUserPageRequest pageRequest) {

    return mapper.queryPage(pageRequest);
    }

    @Override
    public int create(SysUser model) {
    String salt= RandomUtil.generateString(8);
        model.setSalt(salt);
        model.setPassword(Md5HashUtil.getMd5(model.getPassword(),salt));
        return mapper.insert(model);
    }
    @Override
    public int update(SysUser model){
        if(!StringUtils.isEmpty(model.getPassword())){
            String salt= RandomUtil.generateString(8);
            model.setSalt(salt);
            model.setPassword(Md5HashUtil.getMd5(model.getPassword(),salt));
        }
        QueryWrapper<SysUser> queryWrapper=new QueryWrapper();
        queryWrapper.eq("username",model.getUsername());
        return mapper.update(model,queryWrapper);
    }

    @Override
    public SysUser findByName(String name) {
        return dao.getByUsername(name);
    }
}